package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.WdtQybOrderDetailMapper;
import com.ygqh.baby.mapper.WdtQybOrderMapper;
import com.ygqh.baby.model.PreSaleRemind;
import com.ygqh.baby.po.*;
import com.ygqh.baby.service.WdtQybOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
@Service
public class WdtQybOrderServiceImpl implements WdtQybOrderService {

    @Autowired
    private WdtQybOrderMapper wdtQybOrderMapper;
    @Autowired
    private WdtQybOrderDetailMapper wdtQybOrderDetailMapper;

    @Override
    public List<WdtQybOrder> find() {
        WdtQybOrderExample example = new WdtQybOrderExample();
        example.createCriteria();
        return wdtQybOrderMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        WdtQybOrderExample example = new WdtQybOrderExample();
        example.createCriteria();
        return wdtQybOrderMapper.countByExample(example);
    }

    @Override
    public WdtQybOrder findById(Long id) {
        return wdtQybOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public WdtQybOrder findByTradeId(Long tradeId) {
        return wdtQybOrderMapper.selectByTradeId(tradeId);
    }

    @Override
    public int save(WdtQybOrder wdtQybOrder) {
        return wdtQybOrderMapper.insertSelective(wdtQybOrder);
    }

    @Override
    public int update(WdtQybOrder wdtQybOrder) {
        return wdtQybOrderMapper.updateByPrimaryKeySelective(wdtQybOrder);
    }

    @Override
    public ResultSet<WdtQybOrder> search(QueryInfo queryInfo, String q) {
        List<WdtQybOrder> list = wdtQybOrderMapper.selectSuper(queryInfo, q);
        int count = wdtQybOrderMapper.countSuper(q);
        ResultSet<WdtQybOrder> resultSet = new ResultSet<WdtQybOrder>(count, list);
        return resultSet;
    }

    @Override
    public int saveOrUpdate(WdtQybOrder wdtQybOrder) {
        int r = 0;
        if (wdtQybOrder.getTradeId() != null) {
            r = this.update(wdtQybOrder);
        } else {
            r = this.save(wdtQybOrder);
        }
        return r;
    }

    @Override
    public int addBatch(List<WdtQybOrder> tradeList) {

        if (!CollectionUtils.isEmpty(tradeList)) {
            List<WdtQybOrder> list = new ArrayList<WdtQybOrder>();
            for (int i = 0; i < tradeList.size(); i++) {
                list.add(tradeList.get(i));
                if (i % 100 == 0 || i == tradeList.size() - 1) {
                    wdtQybOrderMapper.insertBatch(list);
                    list.clear();
                }
            }
        }
        return 1;
    }

    @Override
    public Boolean isExist(Long tradeId) {
        WdtQybOrderExample example = new WdtQybOrderExample();
        example.createCriteria().andTradeIdEqualTo(tradeId);
        int count = wdtQybOrderMapper.countByExample(example);
        return count > 0 ? true : false;
    }

    @Override
    public Boolean isExist(String srcTid) {
        WdtQybOrderExample example = new WdtQybOrderExample();
        example.createCriteria().andSrcTidsEqualTo(srcTid);
        int count = wdtQybOrderMapper.countByExample(example);
        return count > 0 ? true : false;
    }

    @Override
    public Boolean isDetailExist(String srcOid, String sprcNo) {
        WdtQybOrderDetailExample detailExample = new WdtQybOrderDetailExample();
        detailExample.createCriteria().andSrcOidEqualTo(srcOid).andSpecNoEqualTo(sprcNo);
        int count = wdtQybOrderDetailMapper.countByExample(detailExample);
        return count > 0 ? true : false;
    }

    @Override
    public Boolean isDetailExist(Integer recId) {
        WdtQybOrderDetailExample detailExample = new WdtQybOrderDetailExample();
        detailExample.createCriteria().andRecIdEqualTo(recId);
        int count = wdtQybOrderDetailMapper.countByExample(detailExample);
        return count > 0 ? true : false;
    }

    @Override
    public int addDetailBatch(List<WdtQybOrderDetail> detailExistList) {

        if (!CollectionUtils.isEmpty(detailExistList)) {
            List<WdtQybOrderDetail> list = new ArrayList<WdtQybOrderDetail>();
            for (int i = 0; i < detailExistList.size(); i++) {
                list.add(detailExistList.get(i));
                if (i % 100 == 0 || i == detailExistList.size() - 1) {
                    wdtQybOrderDetailMapper.insertBatch(list);
                    list.clear();
                }
            }
        }
        return 1;
    }

    @Override
    public int updateDetailBatch(List<WdtQybOrderDetail> detailExistList) {
        if (!CollectionUtils.isEmpty(detailExistList)) {
            List<WdtQybOrderDetail> list = new ArrayList<WdtQybOrderDetail>();
            for (int i = 0; i < detailExistList.size(); i++) {
                list.add(detailExistList.get(i));
                if (i % 100 == 0 || i == detailExistList.size() - 1) {
                    wdtQybOrderDetailMapper.updateDetailBatch(list);
                    list.clear();
                }
            }
        }
        return 1;
    }

    @Override
    public List<PreSaleRemind> findSaleCountBySku(List<String> skuList) {
        return wdtQybOrderDetailMapper.selectSaleCountBySku(skuList);
    }

    @Override
    public List<BiSkuSale> statisticalSkuSaleCount(QueryInfo queryInfo, Date startDate, Date endDate, String shopNo,
                                                   String productCode) {

        return wdtQybOrderDetailMapper.statisticalSkuSaleCount(queryInfo, startDate, endDate, shopNo, productCode);
    }

    @Override
    public List<WdtQybOrderDetail> findDetailListAfterId(Long startId, String... excludeShopNo) {
        return wdtQybOrderDetailMapper.selectDetailListAfterId(startId, excludeShopNo);
    }

    @Override
    public List<WdtQybOrderDetail> findDetailListByTime(Date startTime, Date endTime, String... shopNo) {
        return wdtQybOrderDetailMapper.selectDetailListByTime(startTime, endTime, shopNo);
    }

    @Override
    public String getShopName(String shopNo) {
        return wdtQybOrderMapper.getShopName(shopNo);
    }

    @Override
    public List<Map<String, Object>> finSalesReportBySku(Date startTime, Date endTime) {
        return wdtQybOrderMapper.selectSalesReportBySku(startTime, endTime);
    }
}
